package com.ricchieri.manage.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.springframework.dao.DataAccessException;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.datamodel.UserEmailDataModel;
import com.ricchieri.datamodel.UserPhoneDataModel;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.model.Role;
import com.ricchieri.model.UserEmail;
import com.ricchieri.model.UserPhone;
import com.ricchieri.model.UserRole;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "editUserMB")
@ViewScoped
public class EditUserManageBean extends CommonUserManageBean {

	private static final long serialVersionUID = -4431457088573798836L;	
	
	private UserFilter userFilter;
	
	@PostConstruct
	@Override
	public void init() {
		super.init();
		
		userFilter = (UserFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
		loadUserData();
	}


	public void loadUserData() {
		this.setEmailDataModel(new UserEmailDataModel(this.getUser().getActiveUserEmail()));
		this.setEmails(this.getUser().getActiveUserEmail());

		this.setPhoneDataModel(new UserPhoneDataModel(this.getUser().getActiveUserPhone()));
		this.setPhones(this.getUser().getActiveUserPhone());

		this.setDocumentType(String.valueOf(this.getUser().getDocumentType().getId()));
		
		this.setPreviousDni(this.getUser().getDni());
		
		for (Role role : this.getUser().getActiveRoles()) {
			this.getSelectedRoles().add(String.valueOf(role.getId()));
		}

		for (UserEmail userEmail : this.getEmails()) {
			if(userEmail.getIsDefault()) {
				this.setSelectedUserEmail(userEmail);
				break;
			}
		}
		
		for (UserPhone userPhone : this.getPhones()) {
			if(userPhone.getIsDefault()) {
				this.setSelectedUserPhone(userPhone);
				break;
			}
		}
	}
	
	
	@Override
	public String save() {
		try {
			if(validateForm()) {
				this.setDefaultEmail();
				this.setDefaultPhone();
				
				getUser().setDocumentType(this.obtainDocumentType(getDocumentType()));
				getUser().setUserEmail(checkNewEmails());
				getUser().setUserPhone(checkNewPhones());
				
				getUser().setRoles(checkNewRoles());
				
				getUserService().updateUser(getUser());
				FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, getUser());
				return BusinessSettings.VIEW_DETAILS;
			} else {
				return BusinessSettings.UNSUCCESS;
			}
			
		} catch (DataAccessException e) {
			return BusinessSettings.UNSUCCESS;
		}
	}
	
	private List<UserRole> checkNewRoles() {
		List<UserRole> roles = new ArrayList<UserRole>();
		boolean existRole = Boolean.TRUE;
				
		for (UserRole userRole : getUser().getRoles()) {
			existRole = Boolean.FALSE;
			if(userRole.isActive()) {
				for (String role : getSelectedRoles()) {
					if(role.equals(String.valueOf(userRole.getRole().getId()))) {
						existRole = Boolean.TRUE;
					}
				}
			}
			if (!existRole) {
				userRole.setFinishDate(new Date(System.currentTimeMillis()));
				userRole.setActive(Boolean.FALSE);
			}
			roles.add(userRole);
		}
		
		existRole = Boolean.FALSE;
		for (String role : getSelectedRoles()) {
			existRole = Boolean.FALSE;
			for (UserRole userRole : getUser().getRoles()) {
				if(userRole.isActive() && role.equals(String.valueOf(userRole.getRole().getId()))) {
					existRole = Boolean.TRUE;
				}
			}
			if (!existRole) {
				roles.add(new UserRole(this.obtainRole(role), getUser()));
			}
		}
		return roles;
	}
	
	private List<UserEmail> checkNewEmails() {
		List<UserEmail> newUserEMail = new ArrayList<UserEmail>();
		boolean existEmail = Boolean.TRUE;
				
		for (UserEmail userEmail : getUser().getActiveUserEmail()) {
			existEmail = Boolean.FALSE;
			for (UserEmail viewUserEmail : this.getEmails()) {
				if(viewUserEmail.getId() == userEmail.getId()) {
					existEmail = Boolean.TRUE;
				}
			}

			if (!existEmail) {
				userEmail.setActive(Boolean.FALSE);
			} else {
				getEmails().remove(userEmail);
			}
			newUserEMail.add(userEmail);
		}

		for (UserEmail viewUserEmail : this.getEmails()) {
			newUserEMail.add(viewUserEmail);
		}
		
		return newUserEMail;
	}
	
	
	private List<UserPhone> checkNewPhones() {
		List<UserPhone> newUserPhone = new ArrayList<UserPhone>();
		boolean existPhone = Boolean.TRUE;
				
		for (UserPhone userPhone : getUser().getActiveUserPhone()) {
			existPhone = Boolean.FALSE;
			for (UserPhone viewUserPhone : this.getPhones()) {
				if(viewUserPhone.getId() == userPhone.getId()) {
					existPhone = Boolean.TRUE;
				}
			}

			if (!existPhone) {
				userPhone.setActive(Boolean.FALSE);
			} else {
				getPhones().remove(userPhone);
			}
			newUserPhone.add(userPhone);
		}
		
		for (UserPhone viewUserPhone : this.getPhones()) {
			newUserPhone.add(viewUserPhone);
		}
		
		return newUserPhone;
	}
	

	@Override
	protected String getFormName() {
		return "editUserForm";
	}


	@Override
	public Boolean isRequeriedUser() {
		return Boolean.TRUE;
	}
	
	public String doBackToSearchUser() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, userFilter);
		return BusinessSettings.SEARCH_USER;
	}
}
